package com.epam.traning.taskmanager.adapter;

import java.util.List;
import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.epam.traning.taskmanager.R;
import com.epam.traning.taskmanager.images.ImageLoader;

public class TaskAdapter extends ArrayAdapter<JSONObject> {

	public TaskAdapter(Context ctx, List<JSONObject> list) {
		super(ctx, R.layout.adapter_task, list);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		JSONObject task = getItem(position);
		if (convertView == null) {
			convertView = View.inflate(getContext(), R.layout.adapter_task, null);
		}
		TextView textView1 = (TextView) convertView.findViewById(android.R.id.text1);
		TextView textView2 = (TextView) convertView.findViewById(android.R.id.text2);
		
		ImageView imageView = (ImageView) convertView.findViewById(android.R.id.text2);
		ImageLoader.getInstance().bind(this, imageView, "http://some.url.com/image.jpg?uuid="+UUID.randomUUID().getLeastSignificantBits());
		
		try {
			textView1.setText(task.getString("title") +"\n" +task.getString("content"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		textView2.setText(String.valueOf(position));
		return convertView;
	}
	
}
